<?php

/**
* AdminEditCodeTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';
require_once '../framework/Yt.class.php';




class  AdminEditCodeTest extends WebTestCase
{
    private $testName,$testDesc,$testNameNew,$testDescNew,$url,$code,$testGeocode,$testGeocodeNew;
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";
        $this->code = substr(md5(uniqid(rand(), true)),0,6);

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];
        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $this->testNameNew = md5(uniqid(rand(), true));
        $this->testDescNew = md5(uniqid(rand(), true));
        $this->testGeocode = md5(uniqid(rand(), true));
        $this->testGeocodeNew = md5(uniqid(rand(), true));
        $locale =  &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->locale = $locale['default_locale']; 
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");

        $this->get($this->url.'?cmd=AdminAddCode');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");


    }

    public function testEditTextOnly()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$dbh->lastInsertId());

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');

        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );
        $this->assertFalse( $this->setFieldByName("delete_video_file", 1 ) );
        
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide_desc.desc = \"".$this->testDescNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
           
        
    }

    public function testEditTextOnlyAndCheckTranslation()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$dbh->lastInsertId());

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);

        $this->get($this->url.'?cmd=AdminAddCode&locale=en_GB&id='.$dbh->lastInsertId());
        $this->assertTitle('Code hunt Admin');
        $this->assertField('name', '');
        $this->assertField('desc', '');

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Save");

        $this->get($this->url.'?cmd=AdminAddCode&locale=en_GB&id='.$lastInsertId);
        $this->assertTitle('Code hunt Admin');
        $this->assertField('name', $this->testNameNew);
        $this->assertField('desc', $this->testDescNew);
        
    }    

    public function testEditTextAndPic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $dbh->exec("UPDATE guide SET  pic_file = '".$lastInsertId."image.jpg' WHERE rowid=".$lastInsertId);

        copy( dirname(__FILE__).'/image.jpg', $upload['upload_dir'].$lastInsertId.'image.jpg');
        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->setFieldByName("delete_pic_file", 1 );
        $this->assertText($lastInsertId.'image.jpg'); 
        $this->assertPattern('/send_image.php\?id='.$lastInsertId.'image.jpg/');
        
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/imagenew.jpg'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide_desc.desc = \"".$this->testDescNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.pic_file = '".$lastInsertId."imagenew.jpg' ");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(is_file($upload['upload_dir'].$lastInsertId.'imagenew.jpg'));
    }
    
    public function testEditTextAndVideo()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");


        //Upload file to YouTube

        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.3gp");
        $myVideoEntry->setVideoDescription("video.3gp");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.3gp");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.3gp",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE guide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);
        

        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 0 );
        $this->assertText('Wideo w trakcie przetwarzania');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/videonew.3gp'));
        $this->assertTrue($this->setFieldByName('yt_id',''));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));    
   
        $this->setConnectionTimeout(30);
        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");
        $sth = $dbh->prepare("SELECT guide.video_file 
            FROM guide,guide_name 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.geocode = '".$this->testGeocodeNew."'
            AND guide.video_file NOT NULL " );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data,"Ten film wideo nie został jeszcze przetworzony."));
        $this->assertTrue(strpos($data,$this->testNameNew));
    }
 

    public function testEditDeleteText()
    {

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$dbh->lastInsertId());

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 

        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );

        
        $this->assertTrue($this->setFieldByName('name', ''));       
        $this->assertTrue($this->setFieldByName('desc', '' ));    

        $this->click("Zapisz");

        $this->assertText("Podaj nazwę");
  
    }

    public function testEditDeletePic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       


        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $dbh->exec("UPDATE guide SET  pic_file = '".$lastInsertId."image.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/image.jpg', $upload['upload_dir'].$lastInsertId.'image.jpg');
        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->setFieldByName("delete_pic_file", 1 );
        $this->assertText($lastInsertId.'image.jpg'); 

        

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.pic_file IS NULL " );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);


        $this->assertFalse(is_file($upload['upload_dir'].$lastInsertId.'image.jpg'));
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertNoText("Usuń zdjęcie");

    }


   public function testFileSizeSet()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('MAX_FILE_SIZE',"2097152");
    }

    public function testEditDeleteVideo()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $lastInsertId = $dbh->lastInsertId();
        
        //Upload video to YT
        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.3gp");
        $myVideoEntry->setVideoDescription("video.3gp");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.3gp");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD'); 
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.3gp",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);

        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE guide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);
        //////////////////////////////
        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 1 );
        $this->assertText('Wideo w trakcie przetwarzania');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->setConnectionTimeout(30);
        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.video_file IS NULL " );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$matches[1]);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);
        //Get location 
        preg_match('/Location: (.*)\r\n/',$data,$matches);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $matches[1]."&hl=pl");
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);

        $this->assertTrue(strpos($data,"Ten film wideo został usunięty przez użytkownika."));

        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertNoText("Usuń video");        
    }
    
    public function testShowLinkToAlreadyUploadedVideoByYtId()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");


        $dbh->exec("UPDATE guide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$lastInsertId);


        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 0 );
        // create a new VideoEntry object
        $yt = Yt::getClient();
        $videoEntry = new Zend_Gdata_YouTube_VideoEntry();
        $videoEntry = $yt->getVideoEntry('jNQXAC9IVRw');
        foreach ($videoEntry->mediaGroup->content as $content) {
            if ($content->type === "video/3gpp" && $content->format === '6') {
                $this->assertPattern('/rtsp.*video.3gp.*>Pobierz video<\/a>/');
            }
        }


        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $dbh->exec("UPDATE guide SET  video_file = null WHERE rowid=".$lastInsertId);
    }


    public function testUpdateAndResolveGeocode()
    {
        
        $location = "mirsk,pl";

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$dbh->lastInsertId());

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );
        $this->assertFalse( $this->setFieldByName("delete_video_file", 0 ) );

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('geocode', $location ));    


        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE  geocode = :geocode  and rowid = :rowid");
        
        $sth->bindParam(":geocode",$location,PDO::PARAM_STR);
        $sth->bindParam(":rowid",$dbh->lastInsertId(),PDO::PARAM_INT);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1); 
    }

    public function testUpdateAndDoNotResolveGeocode()
    {
        
        $location = "MagicPlaceLocatedOnMars";

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        
        $dbh->exec("INSERT INTO guide ( code,geocode, owner ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$dbh->lastInsertId());

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );
        $this->assertFalse( $this->setFieldByName("delete_video_file", 0 ) );

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('geocode', $location ));    


        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE geocode = :geocode and rowid = :rowid ");
        
        $sth->bindParam(":geocode",$location,PDO::PARAM_STR);
        $sth->bindParam(":rowid",$dbh->lastInsertId(),PDO::PARAM_INT);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1); 
    }

    public function testEditTextOnlyAndPublish()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        
        $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->assertField('publish');

        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );
        $this->assertFalse( $this->setFieldByName("delete_video_file", 1 ) );
        $this->assertTrue( $this->setFieldByName("publish", 1 ) );

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide_desc.desc = \"".$this->testDescNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.publish = 1");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
           
        
    }

    public function testEditTextOnlyAndDontPublish()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->assertField('publish');

        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );
        $this->assertFalse( $this->setFieldByName("delete_video_file", 1 ) );
        $this->setFieldByName("publish",0);

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide_desc.desc = \"".$this->testDescNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.publish = ''");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
           
        
    }

    public function testEditTextAndVideoPublishUnpublish()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $lastInsertId = $dbh->lastInsertId();

        //Upload file to YouTube

        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.3gp");
        $myVideoEntry->setVideoDescription("video.3gp");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.3gp");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.3gp",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE guide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);

        sleep(15);        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 0 );
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));   
        $this->setFieldByName('publish',0); 
   
        $this->setConnectionTimeout(30);
        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");
        $sth = $dbh->prepare("SELECT guide.video_file 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide_desc.desc = \"".$this->testDescNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.video_file IS NOT NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data,"ServiceLogin"));

    }

    public function testEditTextAndVideoUnPublishPublish()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        //Upload file to YouTube

        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.3gp");
        $myVideoEntry->setVideoDescription("video.3gp");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.3gp");
        $myVideoEntry->setVideoPrivate();

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.3gp",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE guide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);

        sleep(15);        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 0 );
        $this->assertText('Wideo w trakcie przetwarzania');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));   
        $this->setFieldByName('publish',1); 
   
        $this->setConnectionTimeout(30);
        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");
        $sth = $dbh->prepare("SELECT guide.video_file 
            FROM guide,guide_name,guide_desc 
            WHERE guide_name.name = \"".$this->testNameNew."\"
            AND guide_desc.desc = \"".$this->testDescNew."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.video_file IS NOT NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);

        sleep(1);
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data,"video.3gp"));

    }
    public function testShowLinkToAlreadyUploadedVideo()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $dbh->exec("UPDATE guide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$lastInsertId);


        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 

        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 0 );
        // create a new VideoEntry object
        $yt = Yt::getClient();
        $videoEntry = new Zend_Gdata_YouTube_VideoEntry();
        $videoEntry = $yt->getVideoEntry('jNQXAC9IVRw');
        foreach ($videoEntry->mediaGroup->content as $content) {
            if ($content->type === "video/3gpp" && $content->format === '6') {
                $this->assertPattern('/rtsp.*video.3gp.*>Pobierz video<\/a>/');
            }
        }


        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $dbh->exec("UPDATE guide SET  video_file = null WHERE rowid=".$lastInsertId);
    }

    public function testEditDeleteVideoNotFromAccount()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
       $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $dbh->exec("UPDATE guide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$lastInsertId);

        
        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 

        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 1 );

        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );



        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        
        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.geocode = '".$this->testGeocode."'
            AND guide.video_file IS NULL " );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertNoText("Usuń video");        
    }

    public function testEditDeleteVideoNotFromAccountEmptyYtId()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
        $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $dbh->exec("UPDATE guide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$lastInsertId);

        
        
        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 

        $this->assertField('video_file');
        $this->setFieldByName("yt_id", '' );

        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );



        $this->click("Zapisz");

        $this->assertText("Kod zaktualizowany.");

        
        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id
            AND guide.geocode = '".$this->testGeocode."'
            AND guide.video_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $this->get($this->url.'?cmd=AdminAddCode&id='.$lastInsertId);
        $this->assertNoText("Usuń video");        
    }

    public function testRemoveCodeWithVideoNotFromAccount()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
 $dbh->exec("INSERT INTO guide ( code,geocode, owner, publish ) VALUES ('".$this->code."','".$this->testGeocode."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $dbh->exec("UPDATE guide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$lastInsertId);

        $this->get($this->url.'?cmd=AdminRemoveCode&id='.$lastInsertId);

        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertText('Usunąć kod "'.$this->testName.'"');

        $this->assertField('Submit', 'Tak');
        $this->assertField('cancel', 'Nie');

        $this->click("Tak");

        $this->assertText("Kod usunięty.");

        $sth = $dbh->prepare("SELECT COUNT(guide.ROWID) 
            FROM guide,guide_name 
            WHERE guide_name.name = \"".$this->testName."\"
            AND guide.code IS NOT NULL 
            AND length(guide.code) = 6
            AND guide.rowid = guide_name.guide_id");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }




    public function tearDown() 
    {
        
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 
        $sth = $dbh->prepare("SELECT guide.pic_file,guide.video_file,guide.rowid 
                                    FROM guide
                                    WHERE guide.owner = '".$this->username."'");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        
        
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }


        if ( $result['video_file'] != '' )
        {
            $yt = Yt::getClient();
            try
            {
                $videoEntry = $yt->getVideoEntry($result['video_file'],null,true);
                $yt->delete($videoEntry);
            }
            catch(Zend_Gdata_App_HttpException $e)
            {
            }
        }

        //if code was added delete translations of name and descryption
        if ( $result['rowid'] > 0 )
        {
    
            $sth = $dbh->prepare("DELETE FROM guide_name WHERE guide_id = ".$result['rowid']);
            $sth->execute();

            $sth = $dbh->prepare("DELETE FROM guide_desc WHERE guide_id = ".$result['rowid']);
            $sth->execute();        
        }

        //Delete all code beleongs to username
        $sth = $dbh->prepare("DELETE FROM guide WHERE owner = \"".$this->username."\"");
        $sth->execute();

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminAddCode&logout=1');
    }        
    
    
}
?>
